package cn.mujiankeji.mbrowser.功能.播放框

import android.annotation.SuppressLint
import android.widget.FrameLayout
import androidx.annotation.OptIn
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.FilterChip
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.media3.common.util.UnstableApi
import androidx.media3.ui.PlayerView
import cn.mujiankeji.mbrowser.功能.播放框.模型.toResizeMode
import cn.mujiankeji.mbrowser.功能.播放框.模型.toStr
import cn.mujiankeji.mbrowser.功能.播放框.模型.播放框画面布局枚举
import cn.mujiankeji.mbrowser.功能.播放框.模型.播放框手势状态
import cn.mujiankeji.mbrowser.功能.播放框.模型.播放框视图状态
import cn.mujiankeji.mbrowser.功能.播放框.模型.播放状态枚举
import cn.mujiankeji.mbrowser.工具.clickable2
import cn.mujiankeji.mbrowser.工具.mlog
import cn.mujiankeji.mbrowser.界面.图标.MyIconPack
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.上锁
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.下一个
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.全屏
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.太阳
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.后退
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.开锁
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.播放
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.旋转
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.暂停
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.画中画
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.设置
import cn.mujiankeji.mbrowser.界面.图标.myiconpack.音量上
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.math.abs


@Composable
fun 播放框视图实现(state : 播放框视图状态<*>){
    state.coroutine = rememberCoroutineScope()

    LaunchedEffect(Unit) {
        state.player.总时长
    }

    // 自动隐藏导航栏
    LaunchedEffect(state.显示导航栏) {
        while (true) {
            delay(500) // 每500毫秒检查一次
            state.检查导航栏显示状态()
            state.检查手势状态显示()
        }
    }

    // 更新进度
    LaunchedEffect(Unit) {
        state.player.播放状态.collect{
            if (it == 播放状态枚举.播放中){
                launch {
                    state.player.更新进度()
                }
            }
        }
    }


    if (state.全屏模式){
        全屏视图(state)
    }
    else if (state.画中画模式){
        画中画视图(state)
    }
    else {
        默认小屏视图(state)
    }

}

@Composable
private fun 默认小屏视图(state : 播放框视图状态<*>){
    Box(Modifier.fillMaxSize().pointerInput(Unit) {
            // 添加手势检测
            detectTapGestures(
                onDoubleTap = { offset ->
                    // 双击切换播放/暂停状态
                    state.切换播放状态()
                },
                onTap = { offset ->
                    // 单击显示/隐藏导航栏
                    state.显示导航栏并自动隐藏()
                }
            )
        }){
        
        // 视频播放器
        exoplayer视图(state)
        
        // 添加手势控制层
        播放器手势控制层(state)



        // 底部工具栏
        if (!state.锁屏状态 && state.显示导航栏){
            Box(Modifier.align(Alignment.BottomStart).fillMaxWidth())
            {
                底部导航栏(state)
            }
        }
    }
    

}

@Composable
private fun 画中画视图(state : 播放框视图状态<*>){
    // 画中画模式下只显示视频画面，不显示任何控制栏
    Box(Modifier.fillMaxSize()) {
        exoplayer视图(state)
        
        // 画中画模式下也添加基本的点击手势，但不显示控制UI
        Box(
            modifier = Modifier
                .fillMaxSize()
                .pointerInput(Unit) {
                    detectTapGestures(
                        onDoubleTap = { offset ->
                            // 双击切换播放/暂停状态
                            state.切换播放状态()
                        }
                    )
                }
        )
    }
}

@Composable
private fun 全屏视图(state : 播放框视图状态<*>){

    Box(Modifier.fillMaxSize()){
        exoplayer视图(state)

        // 添加手势控制层
        播放器手势控制层(state)

        // 顶部工具栏
        if (!state.锁屏状态 && state.显示导航栏){
            Box(Modifier.fillMaxWidth()){
                全屏顶部导航栏(state)
            }
        }

        // 中间
        // 上锁/解锁
        if (state.显示导航栏){
            Box(Modifier
                .padding(start = 65.dp)
                .align(Alignment.CenterStart)
                .clip(CircleShape)
                .background(Color(0x33000000))
                .clickable {
                    state.切换锁屏状态()
                }
                .padding(15.dp)) {
                Icon(if (state.锁屏状态) MyIconPack.上锁 else MyIconPack.开锁,"",
                    tint = Color.White)
            }
        }

        // 翻转
        if (!state.锁屏状态 && state.显示导航栏){
            Box(Modifier
                .padding(end = 65.dp)
                .padding(horizontal = 20.dp)
                .align(Alignment.CenterEnd)
                .clip(CircleShape)
                .background(Color(0x33000000))
                .clickable {
                    state.翻转屏幕方向()
                }.padding(15.dp)) {
                Icon(MyIconPack.旋转,"",
                    tint = Color.White)
            }
        }

        if (state.显示设置){
            设置视图(state)
        }
        else if (state.显示选集){
            选集视图(state)
        }

        // 底部工具栏
        if (!state.锁屏状态 && state.显示导航栏){
            Box(Modifier.align(Alignment.BottomStart).fillMaxWidth()){
                底部导航栏(state)
            }
        }
    }


}

@OptIn(UnstableApi::class)
@Composable
private fun exoplayer视图(state: 播放框视图状态<*>) {

    DisposableEffect(Unit) {
        onDispose {
            state.player.暂停()
        }
    }
    Box(Modifier.fillMaxSize()){
        AndroidView(
            factory = { context ->
                mlog("factory")

                PlayerView(context).apply {
                    player = state.player.播放器
                    useController = false
                    layoutParams = FrameLayout.LayoutParams(
                        FrameLayout.LayoutParams.MATCH_PARENT,
                        FrameLayout.LayoutParams.MATCH_PARENT
                    )
                    resizeMode = state.画面布局样式.toResizeMode()
                }
            },
            update = { view ->
                view.player = state.player.播放器
                view.resizeMode = state.画面布局样式.toResizeMode()
                // update
                mlog("update..")

            }
        )
        
        // 显示加载信息 - 仅显示最后三条
        if (state.当前项目加载信息.size > 0) {
            val 最近信息 = state.当前项目加载信息.takeLast(3)
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp)
                    .align(Alignment.TopStart)
            ) {
                Column {
                    最近信息.forEach { 信息 ->
                        // 为每条信息添加动画效果
                        var 动画偏移 by remember { mutableStateOf(0f) }
                        
                        LaunchedEffect(信息) {
                            // 简单的动画效果，让文字稍微移动以产生动态感
                            while (true) {
                                delay(100)
                                动画偏移 = (动画偏移 + 0.5f) % 5f
                            }
                        }
                        
                        Box(
                            modifier = Modifier
                                .padding(vertical = 2.dp)
                                .offset(x = 动画偏移.dp)
                        ) {
                            Text(
                                text = 信息,
                                style = MaterialTheme.typography.labelSmall,
                                maxLines = 2,
                                color = Color.White,
                                modifier = Modifier
                                    .background(Color(0x66000000), RoundedCornerShape(4.dp))
                                    .padding(horizontal = 8.dp, vertical = 2.dp)
                            )
                        }
                    }
                }
            }
        }
        
        // 显示缓冲加载动画
        val 播放状态 = state.player.播放状态.collectAsState().value
        if (播放状态 == 播放状态枚举.加载中) {
            Box(
                modifier = Modifier.fillMaxSize(),
                contentAlignment = Alignment.Center
            ) {
                Box(
                    modifier = Modifier
                        .size(100.dp)
                        .clip(RoundedCornerShape(8.dp))
                        .background(Color(0x99000000)),
                    contentAlignment = Alignment.Center
                ) {
                    Column(
                        horizontalAlignment = Alignment.CenterHorizontally,
                        modifier = Modifier.padding(16.dp)
                    ) {
                        androidx.compose.material3.CircularProgressIndicator(
                            modifier = Modifier.size(40.dp),
                            color = MaterialTheme.colorScheme.primary,
                            strokeWidth = 3.dp
                        )
                        Spacer(modifier = Modifier.height(8.dp))
                        Text(
                            text = "缓冲中...",
                            color = Color.White,
                            style = MaterialTheme.typography.bodyMedium
                        )
                        
                        // 显示缓冲进度
                        val 缓冲进度 = state.player.缓冲进度.collectAsState().value
                        if (缓冲进度 > 0) {
                            Spacer(modifier = Modifier.height(4.dp))
                            Text(
                                text = "$缓冲进度%",
                                color = Color.White,
                                style = MaterialTheme.typography.bodySmall
                            )
                        }
                    }
                }
            }
        }
    }

    // 自动播放
    LaunchedEffect(Unit) {
        if (state.player.播放状态.value == 播放状态枚举.播放中 || state.player.播放状态.value == 播放状态枚举.已暂停){
            state.player.播放()
        }
    }

}

@SuppressLint("UnsafeOptInUsageError")
@Composable
private fun 底部导航栏(state : 播放框视图状态<*>){

    // 获取当前进度和总时长
    val 当前进度 = state.player.当前进度.collectAsState(0L).value
    val 总时长 = state.player.总时长.collectAsState(0L).value
    val 缓冲进度百分比 = state.player.缓冲进度.collectAsState(0).value

    // 计算进度百分比
    val 进度百分比 = if (总时长 > 0) 当前进度.toFloat() / 总时长.toFloat() else 0f
    val 缓冲百分比 = 缓冲进度百分比.toFloat() / 100f

    // 格式化时间文本
    val 播放进度时间文本 = 毫秒转时间文本(当前进度)
    val 视频总长度时间文本 = 毫秒转时间文本(总时长)

    @Composable
    fun btn(icon : ImageVector, clickable : ()->Unit){
        IconButton({
            clickable()
        }) {
            Icon(icon,"",
                tint = Color.White)
        }
    }

    Row(Modifier.fillMaxWidth()
        .height(55.dp)
        .background(
            brush = Brush.verticalGradient(colors = listOf(Color.Transparent,Color(0x99000000)))
        )

        .padding(bottom = 10.dp)
        .padding(horizontal = if (state.全屏模式) 35.dp else 5.dp),
        verticalAlignment = Alignment.CenterVertically)
    {

        btn(if (state.player!!.播放状态.collectAsState(播放状态枚举.初始化)
                .value == 播放状态枚举.播放中) MyIconPack.暂停 else MyIconPack.播放) {
            state.切换播放状态()
        }

        if (state.含下一集){
            btn(MyIconPack.下一个) {
                state.下一集()
            }
        }

        // 判断是否为直播

        if (state.是否为直播()){
            Spacer(Modifier.weight(1f)) // 直播就不显示进度条
        }
        else {
            Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) {
                Text(播放进度时间文本,
                    style = MaterialTheme.typography.bodyMedium,
                    color = Color.White)

                Spacer(Modifier.width(18.dp))

                PlayerSeekBar(
                    modifier = Modifier.weight(1f),
                    进度 = 进度百分比,
                    缓冲 = 缓冲百分比,
                    onSeek = { 百分比 ->
                        // 拖动时更新预览时间
                    },
                    onSeekEnd = { 百分比 ->
                        // 拖动结束，跳转到新进度
                        val 目标位置 = (总时长 * 百分比).toLong()
                        state.player.跳转(目标位置)
                    },
                    主色 = MaterialTheme.colorScheme.primary,
                    缓冲色 = Color(0x66FFFFFF),
                    背景色 = Color(0x33FFFFFF)
                )

                Spacer(Modifier.width(18.dp))

                Text(视频总长度时间文本,
                    style = MaterialTheme.typography.bodyMedium,
                    color = Color.White)
            }
        }

        if (state.全屏模式 && (state.当前播放列表?.size?:0) > 1){
            Text("选集", style = MaterialTheme.typography.bodyMedium,
                color = Color.White,
                modifier = Modifier.padding(horizontal = 10.dp)
                    .clickable {
                        state.显示选集 = !state.显示选集
                    }
                    .padding(horizontal = 10.dp, vertical = 3.dp))
        }
        /*btn(MyIconPack.画中画) {
            state.切换至画中画()
        }*/
        btn(MyIconPack.全屏) {
            if (state.全屏模式) state.取消全屏()
            else state.切换至全屏()
        }
    }
}


/**
 * 将毫秒转换为时间文本（格式：HH:MM:SS 或 MM:SS）
 */
private fun 毫秒转时间文本(毫秒: Long): String {
    if (毫秒 <= 0) return "00:00"
    
    val 总秒数 = 毫秒 / 1000
    val 小时 = 总秒数 / 3600
    val 分钟 = (总秒数 % 3600) / 60
    val 秒 = 总秒数 % 60
    
    return if (小时 > 0) {
        String.format("%02d:%02d:%02d", 小时, 分钟, 秒)
    } else {
        String.format("%02d:%02d", 分钟, 秒)
    }
}

@Composable
private fun 全屏顶部导航栏(state : 播放框视图状态<*>){
    @Composable
    fun btn(icon : ImageVector,clickable : ()->Unit){
        IconButton({
            clickable()
        }) {
            Icon(icon,"",
                tint = Color.White)
        }
    }

    Row(Modifier.fillMaxWidth()
        .height(50.dp)
        .background(
            brush = Brush.verticalGradient(colors = listOf(Color(0x99000000),Color.Transparent))
        )
        .padding(top = 10.dp)
        .padding(horizontal = 35.dp))
    {

        btn(MyIconPack.后退) {
            state.取消全屏()
        }

        Spacer(Modifier.weight(1f))

        btn(MyIconPack.画中画) {
            state.切换至画中画()
        }

        btn(MyIconPack.设置) {
            state.显示设置 = true
        }
    }
}

@Composable
private fun 设置视图(state : 播放框视图状态<*>){
    if (state.player == null) return
    @Composable
    fun line(name : String, content : @Composable ()->Unit){
        Row(verticalAlignment = Alignment.CenterVertically) {
            Text("$name : ", style = MaterialTheme.typography.bodyMedium, color = Color.White)
            Spacer(Modifier.width(5.dp))
            content()
        }
    }

    @Composable
    fun citem(name : String,sel : Boolean,clickable :()->Unit){
        Box(Modifier.clip(MaterialTheme.shapes.extraSmall)
            .padding(horizontal = 5.dp, vertical = 3.dp)
            .then(if (sel) Modifier.border(1.dp,MaterialTheme.colorScheme.primary,MaterialTheme.shapes.extraSmall)
                else Modifier)
            .clickable { clickable() }
            .padding(horizontal = 10.dp)
            .height(38.dp),
            contentAlignment = Alignment.Center)
        {
            Text(name, color = Color.White, style = MaterialTheme.typography.bodyMedium)
        }
    }


    Box(Modifier.fillMaxSize()
        .clickable2 {
            state.显示设置 = false
        }, contentAlignment = Alignment.TopEnd)
    {
        LazyColumn(Modifier.fillMaxWidth(0.6f)
            .fillMaxHeight()
            .background(Color(0x33000000))
            .padding(horizontal = 20.dp, vertical = 50.dp)) {

            // 倍速
            item {
                line("倍速") {
                    LazyRow {
                        items(listOf(0.5f,1.0f,1.25f,1.5f,2.0f,2.5f,3.0f,3.5f,4.0f)){
                            citem(it.toString(), sel = state.player.播放速率.collectAsState(1.0f)
                                .value == it) {
                                state.更新视频倍速.invoke(it)
                                state.player.设置播放速率(it)
                            }
                        }
                    }
                }
            }

            // 画面
            item {
                line("画面") {
                    LazyRow {
                        items(播放框画面布局枚举.entries){
                            citem(it.toStr(), sel = state.画面布局样式 == it) {
                                state.切换画面布局样式(it)
                            }
                        }
                    }
                }
            }

            // 音轨
            item {
                line("音轨") {
                    val 音轨列表 = state.player!!.获取可用音轨()
                    LazyRow {
                        items(音轨列表) { track ->
                            citem(track.标签, sel = false) {
                                state.player!!.切换音轨(track)
                            }
                        }
                    }
                }
            }

            // 字幕
            item {
                line("字幕") {
                    val 字幕列表 = state.player!!.获取可用字幕()
                    LazyRow {
                        items(字幕列表) { subtitle ->
                            citem(subtitle.标签, sel = false) {
                                state.player!!.切换字幕(subtitle)
                            }
                        }
                        item {
                            if (字幕列表.isNotEmpty()){
                                citem("关闭字幕", sel = false) {
                                    state.player!!.切换字幕(null)
                                }
                            }
                        }
                    }
                }
            }

            // 视频信息
            item {
                val 视频格式 = state.player!!.视频信息.collectAsState(null).value?.toString() ?:""
                line("视频格式") {
                    Text(视频格式)
                }
            }






        }

    }
}

@Composable
private fun <T> 选集视图(state: 播放框视图状态<T>){

    @Composable
    fun item(sel : Boolean,name : String,clickable : ()->Unit){
        Box(Modifier.padding(5.dp,3.dp)
            .clickable { clickable() }
            .then(if (sel) Modifier.background(Color(0x66FFFFFF))
            else Modifier.border(1.dp, Color(0x66FFFFFF)))
            .padding(horizontal = 15.dp)
            .height(28.dp),
            contentAlignment = Alignment.Center){
            Text(name, color = Color.White, style = MaterialTheme.typography.bodyMedium)
        }
    }
    val scope = rememberCoroutineScope()
    Box(Modifier.fillMaxSize()
        .clickable2 {
            state.显示选集 = false
        }, contentAlignment = Alignment.TopEnd)
    {
        FlowRow(Modifier.fillMaxWidth(0.6f)
            .padding(vertical = 100.dp)
            .background(Color(0x33000000))
            .fillMaxHeight()) {
            state.当前播放列表?.forEach { item->
                item(item == state.当前播放项目,state.播放源.取标题(item)){
                    scope.launch {
                        state.设置播放项目(state.当前播放的源分类名称?:"",item)
                    }
                }

            }


        }

    }
}

@Composable
fun PlayerSeekBar(
    modifier: Modifier = Modifier,
    进度: Float, // 0f~1f
    缓冲: Float, // 0f~1f
    onSeek: (Float) -> Unit,
    onSeekEnd: ((Float) -> Unit)? = null,
    主色: Color = MaterialTheme.colorScheme.primary,
    缓冲色: Color = Color.Gray,
    背景色: Color = Color(0x33FFFFFF),
    滑块颜色: Color = MaterialTheme.colorScheme.primary,
    滑块半径: Float = 8f
) {
    var 拖动进度 by remember { mutableStateOf(进度) }
    var 正在拖动 by remember { mutableStateOf(false) }
    
    // 更新拖动进度以匹配外部进度值
    LaunchedEffect(进度) {
        if (!正在拖动) {
            拖动进度 = 进度
        }
    }

    Box(
        modifier = modifier
            .height(28.dp)
            .pointerInput(Unit) {
                detectTapGestures() { offset ->
                    val percent = (offset.x / size.width).coerceIn(0f, 1f)
                    拖动进度 = percent
                    onSeek(percent)
                    onSeekEnd?.invoke(percent)
                }
            }
            .pointerInput(Unit) {
                detectDragGestures(
                    onDragStart = {
                        正在拖动 = true
                    },
                    onDragEnd = {
                        正在拖动 = false
                        onSeekEnd?.invoke(拖动进度)
                    }
                ) { change, _ ->
                    val percent = (change.position.x / size.width).coerceIn(0f, 1f)
                    拖动进度 = percent
                    onSeek(percent)
                }
            }
    ) {
        Canvas(modifier = Modifier.fillMaxWidth().height(2.dp).align(Alignment.CenterStart)) {
            val barHeight = size.height / 2
            // 背景
            drawLine(
                color = 背景色,
                start = Offset(0f, barHeight),
                end = Offset(size.width, barHeight),
                strokeWidth = size.height,
                cap = StrokeCap.Round
            )
            // 缓冲
            drawLine(
                color = 缓冲色,
                start = Offset(0f, barHeight),
                end = Offset(size.width * 缓冲, barHeight),
                strokeWidth = size.height,
                cap = StrokeCap.Round
            )
            // 已播放
            drawLine(
                color = 主色,
                start = Offset(0f, barHeight),
                end = Offset(size.width * 拖动进度, barHeight),
                strokeWidth = size.height,
                cap = StrokeCap.Round
            )
            // 滑块
            drawCircle(
                color = 滑块颜色,
                radius = 滑块半径.dp.toPx(),
                center = Offset(size.width * 拖动进度, barHeight)
            )
        }
    }
}

/**
 * 播放器手势控制层
 */
@Composable
fun 播放器手势控制层(state: 播放框视图状态<*>) {
    
    // 获取屏幕密度
    val density = LocalDensity.current.density

    var dragStartOffsetX by remember { mutableStateOf<Float>(0f) }
    
    // 手势检测区域
    Box(
        modifier = Modifier
            .fillMaxSize()
            .pointerInput(Unit) {
                detectTapGestures(
                    onDoubleTap = { offset ->
                        // 双击切换播放/暂停状态
                        state.切换播放状态()
                    },
                    onLongPress = { offset ->
                        state.开始长按倍速()
                    },
                    onTap = { offset ->
                        // 单击显示/隐藏导航栏
                        if (state.显示导航栏) {
                            // 如果导航栏已显示，则隐藏
                            state.显示导航栏 = false
                        }
                        else {
                            // 否则显示并设置自动隐藏
                            state.显示导航栏并自动隐藏()
                        }
                    },
                    onPress = {
                        state.结束长按倍速()
                        tryAwaitRelease()
                        state.结束长按倍速()
                    }
                )
            }
            .pointerInput(Unit) {
                // 监听长按结束事件
                detectDragGestures(
                    onDragStart = { offset ->
                        dragStartOffsetX = offset.x
                        if (state.锁屏状态) return@detectDragGestures
                        // 如果正在长按倍速中，则标记为长按状态
                        
                        // 如果不是长按状态，则执行正常的拖动手势逻辑
                        if (!state.长按倍速中) {
                            state.手势状态.当前手势 = 播放框手势状态.手势类型.无
                        }
                    },
                    onDragEnd = {
                        if (state.锁屏状态) return@detectDragGestures

                        // 如果是长按状态结束，则结束倍速播放
                        if (state.长按倍速中) {
                            state.结束长按倍速()
                        } else {
                            // 否则执行正常的拖动结束逻辑
                            when (state.手势状态.当前手势) {
                                播放框手势状态.手势类型.进度 -> {
                                    state.应用进度调整()
                                }
                                else -> {
                                    // 其他手势类型不需要额外处理
                                }
                            }
                        }
                        // 重置手势状态
                        state.手势状态.当前手势 = 播放框手势状态.手势类型.无
                    }
                ) { change, dragAmount ->
                    if (state.锁屏状态) return@detectDragGestures

                    // 如果是长按状态，不处理拖动
                    if (state.长按倍速中) return@detectDragGestures

                    if (state.手势状态.当前手势 == 播放框手势状态.手势类型.无){
                        if (abs(dragAmount.x) > abs(dragAmount.y)) {
                            state.手势状态.当前手势 = 播放框手势状态.手势类型.进度
                            val 总时长 = state.player.总时长.value
                            val 当前进度 = state.player.当前进度.value
                            if (总时长 > 0) {
                                state.手势状态.当前进度百分比 = 当前进度.toFloat() / 总时长.toFloat()
                                state.手势状态.目标进度百分比 = state.手势状态.当前进度百分比
                            }
                        } else {
                            mlog("dragAmount",dragAmount.x,size.width)
                            if (dragStartOffsetX < size.width * 0.5)  {
                                state.手势状态.当前手势 = 播放框手势状态.手势类型.亮度
                            }
                            else {
                                state.手势状态.当前手势 = 播放框手势状态.手势类型.音量
                            }
                            // 纵向拖动
                        }
                    }
                    else{
                        change.consume()

                        // 根据手势类型处理拖动
                        when (state.手势状态.当前手势) {
                            播放框手势状态.手势类型.亮度 -> {
                                // 上下滑动调节亮度，向上增加，向下减少
                                val 亮度变化 = -dragAmount.y / 100 // 调整系数使得滑动更灵敏
                                state.调整亮度(亮度变化)
                            }
                            播放框手势状态.手势类型.音量 -> {
                                // 上下滑动调节音量，向上增加，向下减少
                                val 音量变化 = -dragAmount.y / 100 // 调整系数使得滑动更灵敏
                                state.调整音量(音量变化)
                            }
                            播放框手势状态.手势类型.进度 -> {
                                // 左右滑动调节进度
                                val 进度变化 = dragAmount.x / (size.width * 5)
                                val 新进度 = (state.手势状态.目标进度百分比 + 进度变化).coerceIn(0f, 1f)
                                state.调整进度(新进度)
                            }
                            else -> {
                                // 无操作
                            }
                        }

                    }
                    

                }
            }
    ) {
        // 显示长按倍速提示
        if (state.长按倍速中) {
            Box(
                modifier = Modifier
                    .align(Alignment.TopCenter)
                    .padding(top = 30.dp, end = 16.dp)
                    .size(100.dp, 60.dp)
                    .clip(RoundedCornerShape(8.dp))
                    .background(Color(0x69000000)),
                contentAlignment = Alignment.Center
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.padding(8.dp,3.dp)
                ) {
                    Text(
                        text = "${state.长按倍速值}x 倍速中",
                        color = MaterialTheme.colorScheme.primary,
                        style = MaterialTheme.typography.bodyMedium
                    )
                }
            }
        }

        // 显示亮度调节UI
        if (state.手势状态.显示亮度调节) {
            Box(
                modifier = Modifier
                    .align(Alignment.Center)
                    .size(150.dp)
                    .clip(RoundedCornerShape(8.dp))
                    .background(Color(0x99000000)),
                contentAlignment = Alignment.Center
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.padding(16.dp)
                ) {
                    Icon(
                        imageVector = MyIconPack.太阳,
                        contentDescription = "亮度",
                        tint = Color.White,
                        modifier = Modifier.size(36.dp)
                    )
                    Spacer(modifier = Modifier.height(16.dp))
                    LinearProgressIndicator(
                        progress = state.手势状态.当前亮度,
                        modifier = Modifier.fillMaxWidth(),
                        color = MaterialTheme.colorScheme.primary,
                        trackColor = Color(0x33FFFFFF)
                    )
                    Spacer(modifier = Modifier.height(8.dp))
                    Text(
                        text = "${(state.手势状态.当前亮度 * 100).toInt()}%",
                        color = Color.White
                    )
                }
            }
        }
        
        // 显示音量调节UI
        if (state.手势状态.显示音量调节) {
            Box(
                modifier = Modifier
                    .align(Alignment.Center)
                    .size(150.dp)
                    .clip(RoundedCornerShape(8.dp))
                    .background(Color(0x99000000)),
                contentAlignment = Alignment.Center
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.padding(16.dp)
                ) {
                    Icon(
                        imageVector = MyIconPack.音量上,
                        contentDescription = "音量",
                        tint = Color.White,
                        modifier = Modifier.size(36.dp)
                    )
                    Spacer(modifier = Modifier.height(16.dp))
                    LinearProgressIndicator(
                        progress = state.手势状态.当前音量,
                        modifier = Modifier.fillMaxWidth(),
                        color = MaterialTheme.colorScheme.primary,
                        trackColor = Color(0x33FFFFFF)
                    )
                    Spacer(modifier = Modifier.height(8.dp))
                    Text(
                        text = "${(state.手势状态.当前音量 * 100).toInt()}%",
                        color = Color.White
                    )
                }
            }
        }
        
        // 显示进度调节UI
        if (state.手势状态.显示进度调节) {
            val 总时长 = state.player.总时长.collectAsState(0).value
            val 目标时间 = (总时长 * state.手势状态.目标进度百分比).toLong()
            val 当前时间 = state.player.当前进度.collectAsState(0).value
            val 时间差 = 目标时间 - 当前时间
            val 时间差文本 = if (时间差 >= 0) "+${毫秒转时间文本(时间差)}" else "-${毫秒转时间文本(-时间差)}"
            
            Box(
                modifier = Modifier
                    .align(Alignment.Center)
                    .size(180.dp, 100.dp)
                    .clip(RoundedCornerShape(8.dp))
                    .background(Color(0x99000000)),
                contentAlignment = Alignment.Center
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.padding(16.dp)
                ) {
                    Row(verticalAlignment = Alignment.CenterVertically) {
                        Text(
                            text = 毫秒转时间文本(当前时间),
                            color = Color.White
                        )
                        Text(
                            text = " → ",
                            color = Color.White
                        )
                        Text(
                            text = 毫秒转时间文本(目标时间),
                            color = MaterialTheme.colorScheme.primary
                        )
                    }
                    Spacer(modifier = Modifier.height(8.dp))
                    Text(
                        text = 时间差文本,
                        color = if (时间差 >= 0) Color.Green else Color.Red
                    )
                    Spacer(modifier = Modifier.height(16.dp))
                    LinearProgressIndicator(
                        progress = state.手势状态.目标进度百分比,
                        modifier = Modifier.fillMaxWidth(),
                        color = MaterialTheme.colorScheme.primary,
                        trackColor = Color(0x33FFFFFF)
                    )
                }
            }
        }
    }
}